﻿using MyMVC.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MyMVC.DAL;

namespace MyMVC.BLL
{
    /// <summary>
    /// 用户登录逻辑
    /// </summary>
    public class UsersService
    {
        /// <summary>
        /// 用户登录逻辑
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public Users Login(Users user) {
            string sql = "select * from Users where UName='{0}' and UPwd='{1}'";
            sql = string.Format(sql,user.UName,user.UPwd);
            SqlDataReader sr = DBHelper.GetReader(sql);
            if (sr.Read())
            {
                user.UName = sr["UName"].ToString();
            }
            else {
                user = null;
            }
            sr.Close();
            return user;
        }

        /// <summary>
        /// 用户注册逻辑
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public Users insert(Users user)
        {
            string sql = "insert into Users values('{0}','{1}','" + System.Guid.NewGuid().ToString() + "');";
            sql = string.Format(sql, user.UName, user.UPwd);
            SqlDataReader sr = DBHelper.GetReader(sql);
            sr.Close();
            return user;
        }

    }
}
